Skip to content

Method: TicTacToePlayerImpl(TicTacToeState, String, boolean)

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel21-tictactoe-core.
5: *
6: * ipspiel21-tictactoe-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel21-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel21-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel21.tictactoe.core.domain.impl;
20:
21: import java.util.Objects;
22: import java.util.Optional;
23:
24: import de.fhdw.gaming.core.domain.PlayerState;
25: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeFieldState;
26: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToePlayer;
27: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeState;
28:
29: /**
30: * Implements {@link TicTacToePlayer}.
31: */
32: final class TicTacToePlayerImpl implements TicTacToePlayer {
33:
34: /**
35: * The associated game state.
36: */
37: private final TicTacToeState gameState;
38: /**
39: * The name of this player.
40: */
41: private final String name;
42: /**
43: * {@code true} if this player uses crosses, or {@code false} if she uses noughts.
44: */
45: private final boolean usingCrosses;
46:
47: /**
48: * Creates a Tic Tac Toe player.
49: *
50: * @param gameState The associated game state.
51: * @param name The name of the player.
52: * @param usingCrosses {@code true} if this player uses crosses, or {@code false} if she uses noughts..
53: */
54: TicTacToePlayerImpl(final TicTacToeState gameState, final String name, final boolean usingCrosses) {
55: this.gameState = Objects.requireNonNull(gameState, "gameState");
56: this.name = Objects.requireNonNull(name, "name");
57: this.usingCrosses = usingCrosses;
58: }
59:
60: @Override
61: public String toString() {
62: return String.format(
63: "TicTacToePlayer[name=%s, type=%s, state=%s, outcome=%s]",
64: this.name,
65: this.isUsingCrosses() ? TicTacToeFieldState.CROSS.toString() : TicTacToeFieldState.NOUGHT.toString(),
66: this.getState(),
67: this.getOutcome());
68: }
69:
70: @Override
71: public boolean equals(final Object obj) {
72: if (obj instanceof TicTacToePlayerImpl) {
73: final TicTacToePlayerImpl other = (TicTacToePlayerImpl) obj;
74: return this.name.equals(other.name) && this.usingCrosses == other.usingCrosses
75: && this.getState().equals(other.getState()) && this.getOutcome().equals(other.getOutcome());
76: }
77: return false;
78: }
79:
80: @Override
81: public int hashCode() {
82: return Objects.hash(this.name, this.usingCrosses);
83: }
84:
85: @Override
86: public String getName() {
87: return this.name;
88: }
89:
90: @Override
91: public boolean isUsingCrosses() {
92: return this.usingCrosses;
93: }
94:
95: @Override
96: public PlayerState getState() {
97: return this.gameState.getPlayerState(this.name);
98: }
99:
100: @Override
101: public Optional<Double> getOutcome() {
102: return this.gameState.getPlayerOutcome(this.name);
103: }
104:
105: @Override
106: public TicTacToePlayer deepCopy(final TicTacToeState newGameState) {
107: return new TicTacToePlayerImpl(newGameState, this.name, this.usingCrosses);
108: }
109: }